<?php
namespace app\user\home;

use app\index\home\Home;
use thans\jwt\facade\JWTAuth;
use app\user\model\CommonUser as CommonUserModel;

/**
 * 前台首页控制器
 * @package app\cms\admin
 */
class Auth extends Home
{

    /**
     * 初始化方法
     */
    protected function initialize()
    {
        parent::initialize();
    }

    // 获取用户信息
    public  function info(){
        $commonId = $this->auth();
        if($commonUser = CommonUserModel::where('id', $commonId)->find()){
            jsuccess($commonUser);
        }else{
            jerror('获取失败');
        }
    }

    // 刷新TOKEN
    public function refresh()
    {
        try {
            $token = JWTAuth::refresh();    //刷新token，会将旧token加入黑名单
            jsuccess($token);
        } catch (\Exception $e) {
            jerror($e->getMessage());
        }
    }

    public function auth()
    {
        try {
            $payload = JWTAuth::auth();
            return $payload['id']->getValue();
        } catch (\thans\jwt\exception\TokenExpiredException $e) {
            //过期 续期使用
            jerror($e->getMessage(), 10002);
        } catch (\thans\jwt\exception\TokenBlacklistException $e) {
            //黑名单 无法使用
            jerror($e->getMessage(), 10003);
        } catch (\Exception $e) {
            //失败 重新授权
            jerror($e->getMessage(), 10001);
        }
    }
}